package com.example.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.pojo.*;
import com.example.service.OrderAdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/OrderController")
public class OrderController {
@Reference
OrderAdminService orderAdminService;
@RequestMapping("/findOrder")
   public String findOrder(HttpSession session,Integer id,Integer num){
    User loginUser = (User) session.getAttribute("loginUser");

    //查询用户购物车里某个商品是否存入
    OrderAdmin orderAdmin = orderAdminService.findById(id);
    if (orderAdmin !=null){
        orderAdminService.edit(orderAdmin);
    }else {
        OrderAdmin orderAdmin1 = new OrderAdmin();
        orderAdmin1.setuName(loginUser.getNickname());
        orderAdmin1.setBuyNumber(num);

        Goods infoGoods = (Goods) session.getAttribute("infoGoods");
        orderAdmin1.setgTitle((String)infoGoods.getTitle());

        System.out.println("name:"+orderAdmin1.getgTitle());
        orderAdmin1.setCurrentPrice(infoGoods.getCurrentPrice());
        orderAdmin1.setTotalPrice(num*orderAdmin1.getCurrentPrice());
        orderAdmin1.setgId(id);
        orderAdmin1.setuId(loginUser.getId());
        orderAdminService.insertOrder(orderAdmin1);
    }
    return "redirect:findUserOrder";
   }
    @RequestMapping("findUserOrder")
    public String findUserOrder(HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");

        List<OrderAdmin> userCarList = orderAdminService.findUserOrder(loginUser.getId());
        int sum=0;
        for (OrderAdmin orderAdmin : userCarList) {

            sum +=orderAdmin.getTotalPrice();
        }
        session.setAttribute("userCarList",userCarList);
        session.setAttribute("allPrice",sum);
        return "redirect:/ListUserCar.jsp";
    }

    @RequestMapping("/deleteBacth")
    public String deleteBacth(String cids){
        String[] split = cids.split(",");
        for (String s : split) {
            orderAdminService.deleteById(Integer.parseInt(s));
        }
    return "redirect:/OrderController/findUserOrder";
    }
    @RequestMapping("jiesuan")
    public String jiesuan (HttpSession session, String cids){
        String[] split = cids.split(",");
        ArrayList<OrderAdmin> orderAdminArrayList = new ArrayList<>();
        int sum =0;
        for (String s : split) {
            OrderAdmin byId = orderAdminService.findById(Integer.parseInt(s));
            orderAdminArrayList.add(byId);
            sum+=byId.getTotalPrice();
        }
        session.setAttribute("jiesuanCar",orderAdminArrayList);
        session.setAttribute("zonge",sum);
        session.setAttribute("cids",cids);
    return "redirect:/SubmitOrders.jsp";
    }
    @ResponseBody
    @RequestMapping("/submitOrders")
    public String submitOrders(HttpSession session,String prov,String city,String country,String address,String cids,String comments){
        User loginUser = (User) session.getAttribute("loginUser");
        orderAdminService.submitOrders(prov,city,country,address,cids,loginUser.getId().toString(),loginUser.getNickname(),comments);
        return "订单支付完毕 请查询第三方支付结果";

    }
}
